package com.jflm.util;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tomcat.jdbc.pool.ClassLoaderUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class PayFactoryTest {
	private static Log _log = LogFactory.getLog(PayFactoryTest.class);
	public class HttpFactory {
		

		
		// WAP页面支付
		private String[] tags = { "FUNCODE","MID","REQDATE","REQTIME","MERID","ORDERID","ORDERDATE","COUPID","ISSUENO","PASSWD","BPID","SUBMERID","AMOUNT","TYPE","COUPNUM","MEDIATYPE","MEDIANO",
				"SHOPNO","POSID","CHECKCHANNEL","OPERATOR",
				"MCODE","DATE","TIME","AMOUT","ALLOWNOTE","AUTHORIZEMODE","CURRENCY","PERIOD",
				"PERIODUNIT","PRODUCTDESC","PRODUCTID","PRODUCTNAME","TXNTYP","CALLBACK","MOBILEID","ACT_ID","MBL_NO","BON_AMT","MLOG_NO","TCNL_TYP","TTXN_DT",
				"PAG_NUM","CHECKSTATE","PAG_NO","REPERTORY","CHANNEL","COUNT","POSID","SMSNO","USERNAME","REMARKS","SENDMAN","SHORTNAME",
				"PROTYPE","PRICE","COST","LEASTCOST","REDUCECOST","COUNTS","VALIDTYPE","EFFECTDATE","EXPIREDATE","VALIDPERIOD","DETAIL","OPERATOR","RETCODE","RETMSG",
		};
		//循环报文中key
		private String[] recs = {  "FUNCODE","MID","REQDATE","REQTIME","MERID","ORDERID","ORDERDATE","COUPID","ISSUENO","PASSWD","BPID","SUBMERID","AMOUNT","TYPE","COUPNUM","MEDIATYPE","MEDIANO",
				"SHOPNO","POSID","CHECKCHANNEL","OPERATOR",
				"MCODE","DATE","TIME","AMOUT","ALLOWNOTE","AUTHORIZEMODE","CURRENCY","PERIOD",
				"PERIODUNIT","PRODUCTDESC","PRODUCTID","PRODUCTNAME","TXNTYP","CALLBACK","MOBILEID","ACT_ID","MBL_NO","BON_AMT","MLOG_NO","TCNL_TYP","TTXN_DT",
				"PAG_NUM","CHECKSTATE","PAG_NO","REPERTORY","CHANNEL","COUNT","POSID","SMSNO","USERNAME","REMARKS","SENDMAN","SHORTNAME",
				"PROTYPE","PRICE","COST","LEASTCOST","REDUCECOST","COUNTS","VALIDTYPE","EFFECTDATE","EXPIREDATE","VALIDPERIOD","DETAIL","OPERATOR",
				XmlPAY.BANKNO, 
				XmlPAY.CARDNO, XmlPAY.NAME, XmlPAY.GENDER
				, XmlPAY.PHONE, XmlPAY.AUTOAREA,
				XmlPAY.AUTOAMOUNT, XmlPAY.DATE, XmlPAY.CANDATE, XmlPAY.STS,
				"ACT_ID","MBL_NO","BON_AMT","MLOG_NO","TCNL_TYP","TTXN_DT","PAG_NUM","CHECKSTATE","PAG_NO","REPERTORY","CHANNEL","COUNT","POSID"};
		
//		@SuppressWarnings("static-access")	
		@SuppressWarnings("unchecked")
		public String doHttpMessage(Map<String, Object> map,boolean isRes) throws Exception {
			StringBuffer sb = new StringBuffer();
            StringBuffer signStr = new StringBuffer();
			if (map.size() != 0) {
				sb.append("<MESSAGE>");
				for (int i = 0; i < tags.length; i++) {
					String key = tags[i];
					Object v = map.get(key);
					if (v == null)
						continue;
					
					if(key.equals(XmlPAY.REC)){
						sb.append("<").append(XmlPAY.REC).append(">");
						List<Map<String, Object>> list = (List<Map<String, Object>>)v;
						for(int j=0; j<recs.length; i++){
							Map recMap = (Map)list;
							String recKey = recs[j];
							Object recValue = recMap.get(recKey);
							if(recValue != null){
								sb.append("<").append(recKey).append(">").append(recValue).append("</").append(recKey).append(">");
							}
						}
						sb.append("</").append(XmlPAY.REC).append(">");
					}else{
						sb.append("<").append(key).append(">").append(v).append("</").append(key).append(">");
					} 
					
					if (isRes){
						signStr.append(v).append("|");
					}else{
						signStr.append(key).append("=").append(v).append("&");
					}
				}
				
				String funcode = (String)map.get("FUNCODE");
				if (funcode == null || "101500".equals(funcode)
						|| "101510".equals(funcode) || "101520".equals(funcode)
						|| "101530".equals(funcode) || "101481".equals(funcode)
						|| "101482".equals(funcode) || "101483".equals(funcode)
						|| "101484".equals(funcode) || "1".equals(funcode) || "2".equals(funcode)
						|| "3".equals(funcode) || "4".equals(funcode) || "5".equals(funcode)
						|| "6".equals(funcode) || "7".equals(funcode)) {
					
				/*}else if ("1".equals(MappingMgr.getInstance().get("SIGN", null))) {*/
				}
				else if (true) {
					 _log.info("请求报文##############");
					String signString = signStr.toString();
					int signStringLen = signString.length();
					signString = signString.substring(0, signStringLen - 1);
					_log.info("报文生成的签名串:"+signString);
					signString= "<" + XmlPAY.SIGN + ">" + SignHandlerTest.doGenerateSign((String)map.get("MERID"),signString)
							+ "</" + XmlPAY.SIGN + ">";
					sb.append(signString);
				}
				sb.append("</MESSAGE>");
			}
			_log.info("报文生成的签名串:"+sb.toString());
			return sb.toString();
		}

	}
}
